home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------------------
-
- PROGRAM
- MenuMem - A combination INIT/DRVR that displays the amount of
- free memory in the menu bar.
-
- FILE
- “MenuMem DRVR.c”
-
- DESCRIPTION
- This file contains all of the code for the DRVR. In addition to the
- standard Open, Control, and Close routines needed by all DRiVeRs, there
- are two support routines, setup_theMenu() and update_theMenu(). This
- DRiVeR does not support read, write or status calls.
-
- HISTORY
- 2/14/90 - DAG Version .1
-
- Copyright © 1990 Daniel A. Green. All rights reserved.
-
- ------------------------------------------------------------------------------ */
-
-
-
- /* --------------------------------- INCLUDE FILES --------------------------- */
- #include "MacTypes.h"
- #include "string.h"
- #include "MenuMgr.h"
- #include <DeviceMgr.h>
- #include <ResourceMgr.h>
- #include "MenuMem.h"
-
-
-
- /* ------------------------------ GLOBAL VARIABLES --------------------------- */
- LongInt free_memory = 0; /* only need two */
- Integer first_close = TRUE;
-
-
-
- /* --------------------------------- FUNCTIONS ----------------------------- */
- main(p, d, i)
- cntrlParam *p; /* ptr to parameter block */
- DCtlPtr d; /* ptr to device control entry */
- int i; /* entry point selector */
- {
- Integer theErr;
-
- if (d->dCtlStorage == 0) { /* couldn’t get our storage */
- if (i == 0)
- CloseDriver(d->dCtlRefNum);
- return(noErr);
- }
-
- switch (i) { /* handle the request: */
- case 0: /* open */
- theErr = OpenDRVR(d, p);
- break;
- case 2: /* control */
- theErr = ControlDRVR(d, p);
- break;
- case 4: /* close */
- theErr = CloseDRVR(d, p);
- break;
- default:
- theErr = noErr;
- break;
- }
-
- return(theErr);
- };
-
-
-
- /*******************************************************************************
- OpenDRVR
- * This routine resets the dCtlFlags, dCtlDelay and dCtlMenu fields of the
- * DCtlPtr associated with the DRiVeR.
- *
- *******************************************************************************/
-
- OpenDRVR(d, p)
- DCtlPtr d;
- cntrlParam *p;
- {
-
- /* Don’t need to respond to Read, Write, or Status calls */
- d->dCtlFlags &= ~(dReadEnable | dWritEnable | dStatEnable);
- /* but we do need time */
- d->dCtlFlags |= dNeedTime | dNeedGoodBye; /* to update the menus */
- d->dCtlDelay = 300; /* Every 5 seconds */
- d->dCtlMenu = OWNEDRSRCID(d->dCtlRefNum);
-
- return(noErr);
- };
-
-
-
- /*******************************************************************************
- setup_theMenu
- * Creates a menu for which the title is the amount of free memory in the
- * application heap (in K), the first menu item is the number of free bytes in
- * the Application Heap (disabled), the second menu item is the number of free
- * bytes in the System Heap (disabled). The fourth menu item, when chosen will
- * compact memory and purge all purgeable blocks from the application (read
- * current) heap by calling the MaxMem trap.
- *
- *******************************************************************************/
-
- void setup_theMenu(d)
- DCtlPtr d;
- {
- char *menuTitle;
- LongInt fm;
- MenuHandle theMenu;
- THz saved_zone;
- Str255 fmStr, item1, item2;
-
- fm = FreeMem() / 1024;
- NumToString(fm, &fmStr);
- menuTitle = strcat( PtoCstr( (char *) &fmStr), "K");
- theMenu = NewMenu(d->dCtlMenu, CtoPstr(menuTitle));
- InsertMenu(theMenu, 0); /* Add menu to the menu list */
-
- saved_zone = GetZone(); /* Save the current zone */
-
- /* Setup the first menu item */
- strcpy((char *) &item1, "(Application Heap: ");
- SetZone( ApplicZone() );
- fm = FreeMem();
- NumToString(fm, &fmStr);
- AppendMenu(theMenu,CtoPstr(strcat((char *) &item1, PtoCstr((char *) &fmStr))));
-
- /* Setup the second menu item */
- strcpy((char *) item2, "(System Heap: ");
- SetZone( SystemZone() );
- fm = FreeMem();
- NumToString(fm, &fmStr);
- AppendMenu(theMenu,CtoPstr(strcat((char *) &item2, PtoCstr((char *) &fmStr))));
-
- /* Set up remaining menu items */
- AppendMenu(theMenu, "\P(-" );
- AppendMenu(theMenu, "\PCompact Memory" );
-
- SetZone(saved_zone);
- DrawMenuBar();
- };
-
-
-
- /*******************************************************************************
- update_theMenu
- * Delete the DRiVeRs current menu and create a new one if the amount of free
- * memory has changed since the last call or we don’t have a menu in the
- * current menu bar.
- *
- *******************************************************************************/
-
- update_theMenu(d)
- DCtlPtr d;
- {
- Boolean not_installed;
- LongInt fm;
- MenuHandle theMenu;
-
- not_installed = ( (theMenu = GetMHandle(d->dCtlMenu)) == NULL );
- fm = FreeMem() / 1024;
- if ( not_installed || (free_memory != fm) ) {
- free_memory = fm;
- if ( theMenu ) {
- DeleteMenu(d->dCtlMenu);
- DisposeMenu(theMenu);
- }
- setup_theMenu(d);
- }
- return(noErr);
- };
-
-
-
- /*******************************************************************************
- ControlDRVR
- * This routine will be called periodically to update the menu and to handle
- * the case when our one menu item is chosen.
- *
- *******************************************************************************/
-
- ControlDRVR(d, p)
- DCtlPtr d;
- cntrlParam *p;
- {
- Integer theErr;
- Size grow, lcf_block;
-
- switch (p->csCode) {
- case accMenu:
- lcf_block = MaxMem(&grow);
- theErr = update_theMenu(d);
- break;
-
- case accRun: /* periodicEvent */
- theErr = update_theMenu(d);
- break;
-
- case goodBye:
- theErr = CloseDRVR(d, p);
- break;
-
- default:
- theErr = noErr;
- break;
- }
-
- return(theErr);
- };
-
-
-
- /*******************************************************************************
- CloseDRVR
- * Whenever we get a close request, we’ll always return an error so we won’t be
- * closed (This method will not work on 64K ROMs).
- * NOTE: The first time we get a goodbye kiss will be after all INITS have
- * executed. The Menu Manager has not been initialized at this time so no
- * calls can be made yet (We don’t have a menu at this point anyway).
- *
- *******************************************************************************/
-
- CloseDRVR(d, p)
- DCtlPtr d;
- cntrlParam *p;
- {
- MenuHandle theMenu;
-
- if ( !first_close ) {
- if (theMenu = GetMHandle(d->dCtlMenu)) {
- DeleteMenu(d->dCtlMenu);
- DisposeMenu(theMenu);
- }
- }
- first_close = FALSE;
- return(closeErr);
- };
-
-